<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 1.x
|  Copyright © 2005-2008, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Libraries - General
*  
* This library handles various general functions for Ineo,
* including sending of watchlist alerts, logging, etc.
*
* @package		Ineo
* @subpackage   Libraries
* @author		Dan Cryer & William McGann
* @copyright	Dan Cryer & William McGann
* @version		1.0.0
*/
class ineo_lib_global
{
  var $depth_guide = "--";
	/**
	* Ineo - Standard Error Handler
	*/
	function error( $error_title = null, $error_message = null , $link = array( 'lang' =>'ams_return_to_idx' , 'link' => 'autocom=ineo' ) )
	{	
		$error_message   = empty($error_message) ? $this->core->error : $error_message;
		$error_title     = empty($error_title) ? $this->ipsclass->lang['ams_error_title'] : $error_title;
		
		$message = empty($this->ipsclass->lang[$error_message]) ? $error_message : $this->ipsclass->lang[$error_message];
		$title   = empty($this->ipsclass->lang[$error_title]  ) ? $error_title : $this->ipsclass->lang[$error_title];
		
		$error = array(
						'title'   => $title,
						'message' => $message,
						'link'    => $link,
						);
		
		$this->core->out .= $this->core->templates['global']->general_error( $error );
	}
	
  function watchlist_alert( $watch_id , $watch_type , $data = '' )
  {
    // Load our PM user:
    $this->ipsclass->DB->simple_construct( array(
    'select' => '*',
    'from'   => 'members',
    'where'  => 'name = \''.$this->ipsclass->vars['ams_alert_user'].'\'',
    )
    );
    $ams = $this->ipsclass->DB->simple_exec();

	// If the user does not exist, they can't send a PM, can they?
	if( !$this->ipsclass->DB->get_num_rows() )
	{
		return;
	}
	
    $ams = $this->ipsclass->DB->fetch_row($ams);

    if( $watch_type == 1 )
    {
      // Article Specific Query:

      $watch['title'] = $this->ipsclass->lang['ams_watch_article'];
      $watch['op']    = 'art';

      $query = array(
      				'select'   => 'w.* , a.article_title',
      				'from'     => array( 'ams_watchlists' => 'w' ),
      				'where'    => 'watch_type = 1 AND watch_id = ' . $watch_id,
      				'add_join' => 
								array( 
									1 => array(
      											'type'     => 'left',
     											'from'     => array( 'ams_articles' => 'a' ),
      											'where'    => 'a.article_id = w.watch_id',
      											)
      									)
      				);

      $this->ipsclass->DB->simple_construct( $query );
    }
    else
    {

      $query = array(
      'select' => '*',
      'from'   => 'ams_articles',
      'where'  => 'article_id = ' . $data,
      );

      $this->ipsclass->DB->simple_construct( $query );
      $this->ipsclass->DB->simple_exec();
      $data = $this->ipsclass->DB->fetch_row();

      // Category Specific Query:

      $watch['title'] = $this->ipsclass->lang['ams_watch_category'];
      $watch['op']    = 'cat';

      $query = array(
      'select' => 'w.* , c.category_name',
      'from'   => array( 'ams_watchlists' => 'w' ),
      'where'  => 'watch_type = 2 AND watch_id = ' . $watch_id,
      'add_join' => array( 1 => array(
      'type' => 'left',
      'from' => array( 'ams_categories' => 'c' ),
      'where' => 'c.category_id = w.watch_id',
      )
      )
      );
      $this->ipsclass->DB->simple_construct( $query );
    }

    $q = $this->ipsclass->DB->simple_exec();


    while( $res = $this->ipsclass->DB->fetch_row($q) )
    {
      // Set options:
      $opts['save_only'] = false;
      $opts['orig_id']   = 0;
      $opts['preview']   = false;
      $opts['track']     = 0;
      $opts['add_sent']  = false;
      $opts['hide_cc']   = true;

      // Load class:
      $msg =& $this->ipsclass->load_class('sources/lib/func_msg.php','func_msg');
      $msg->init();

      // Set to ID:
      $msg->to_by_id = $res['watch_user'];
      $msg->from_member =& $ams;
      $msg->msg_title = $this->ipsclass->lang['ams_alert_title'];

      $data = array_merge( $res , $data , $this->ipsclass->member );

      $msg->msg_post = $this->do_replacements($this->ipsclass->lang['ams_' . $watch['op'] . '_alert'] , $data );
      $msg->send_pm($opts);
    }

  }

  function do_replacements( $msg , $data )
  {
    $data['article_link']  = $this->core->base_url . 'showarticle=' . $data['watch_id'];
    $data['category_link'] = $this->core->base_url . 'showcat=' . $data['watch_id'];

    foreach( $data as $key => $val )
    {
      $msg = str_replace( '{' . $key . '}' , $val , $msg );
    }

    return $msg;
  }
  
  /**
  * Simple 'nuff. Inserts a row in the ams_logs table with a quick note regarding the action.
  */
  function add_log($act, $code, $note)
  {
    $insert = array('member_id' => $this->ipsclass->member['id'],
                    'act' => $act,
                    'code' => $code,
                    'note' => $note,
                    'time' => time(),
                    'ip_address' => $this->ipsclass->input['IP_ADDRESS']);
    $this->ipsclass->DB->do_insert('ams_logs', $insert, 1);
  }

	// Builds the dropdown for category listing.
	function category_dropdown($cid='')
	{
	  $this->ipsclass->DB->simple_construct( array(
	                   'select' => 'category_id, category_name, category_parent',
	                   'from' => 'ams_categories' ) );
    $this->ipsclass->DB->simple_exec();
    
    
    while( $row = $this->ipsclass->DB->fetch_row() )
    {
      $array[$row['category_parent']][$row['category_id']] = $row;
    }
    
    //echo "<pre>";print_r($array);die();
    
    $menu = '<select name="category">';
    $menu .= $this->category_dropdown_internal( '0', $array, $cid );
    $menu .= '</select>';
    
    return $menu;
	}
	
	// Recursivityityity!!
  function category_dropdown_internal($root_id, $array, $selected_id, $jump_string="", $depth_guide="" )
  {
    if ( is_array( $array[ $root_id ] ) )
    {
      foreach( $array[ $root_id ] as $id => $cat_data )
      {
      $selected = "";
      if ( $selected_id == $cat_data['category_id'] )
      {
        $selected = ' selected="selected"';
      }
      
      $prefix = $depth_guide == '' ? '' : '&nbsp;&nbsp;&#0124;' ;
      
      $jump_string .= "<option value=\"{$cat_data['category_id']}\"".$selected.">".$prefix.$depth_guide." ".$cat_data['category_name']."</option>\n";
      $jump_string = $this->category_dropdown_internal( $cat_data['category_id'], $array, $selected_id, $jump_string, $depth_guide . $this->depth_guide );
      }
    }
	  return $jump_string;
  }
	
	// UTF-8 Fixing
	function ajax_utf8_safe()
	{
		// Make sure the charset is set:
		$this->ipsclass->vars['gb_char_set'] = $this->ipsclass->vars['gb_char_set'] ? $this->ipsclass->vars['gb_char_set'] : 'iso-8859-1';
		
		// Make sure it is also passed via header:
		@header( "Content-type: text/html;charset={$this->ipsclass->vars['gb_char_set']}" );
	}
}

?>